<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <el-form :inline="true" class="filter-container filter">
      <div>
        <el-form-item label="出库日期">
          <el-date-picker v-model="alarmdate" type="daterange" range-separator="至" start-placeholder="开始日期"
            end-placeholder="结束日期" />
        </el-form-item>

        <el-form-item label="设备标签" style="margin-left: 10px">
          <div style="display:flex;justify-content:flex-end;align-items:center">
            <el-input @change="caskIdNameChange" v-model="listQuery.caskName" clearable class="filter-item" placeholder="请输入设备标签" />
            <i style="display: inline-block;width:20px;color:#409EFF;margin-left:3px" class="el-icon-search"
              @click="searchDialogcode('search')" />
          </div>
        </el-form-item>
        <el-form-item label="客户名称" style="margin-left: 44px">
          <el-select :disabled="fsiteNoFlag" @change="changeFsiteNo" v-model="listQuery.fsiteNo" clearable filterable
            class="filter-item" placeholder="请选择客户名称">
            <el-option v-for="item in getListfsiteNo" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="listQuery.fstate" clearable filterable class="filter-item" placeholder="请选择状态">
                <el-option v-for="(label,value) in statusMap" :key="value" :label="label"
                  :value="value" />
              </el-select>
        </el-form-item>
      </div>
      <div>
        <el-form-item label="仓库名称" prop="repoId">
          <el-select v-model="listQuery.repoId" clearable filterable class="filter-item"
            placeholder="请选择仓库名称">
            <el-option v-for="item in wmslist" :key="item.fstockno" :label="item.fstockname"
              :value="Number(item.fstockno)" />
          </el-select>
        </el-form-item>
        <el-form-item label="处置企业" prop="enterpriseId" style="margin-left: 80px">
              <el-select v-model="listQuery.enterpriseId" clearable filterable class="filter-item" placeholder="请选择处置企业">
                <el-option v-for="item in enterpriseIdList" :key="item.enterpriseId" :label="item.enterpriseName"
                  :value="item.enterpriseId" />
              </el-select>
            </el-form-item>
        <el-form-item label="过磅人员" style="margin-left: 69px">
          <div style="display:flex;justify-content:flex-start;align-items:center">
            <el-select style="display: inline-block" clearable filterable v-model="listQuery.weightPerson"
              class="filter-item" placeholder="请选择过磅人员">
              <el-option v-for="item in getListforduser" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
            <i class="el-icon-search selectI" @click="searchDialoguse('search')" />
          </div>
        </el-form-item>
      </div>
      <div>
        <el-form-item>
          <el-button round size="medium" class="filter-item"
            :style="{ background: buttonColor.lookup, color: buttonColor.color }" @click="handleFilter">查找</el-button>
          <el-button round size="medium" class="filter-item"
            :style="{ background: buttonColor.add, color: buttonColor.color }" @click="handleCreate">添加</el-button>

          <!-- <el-button round size="medium" class="filter-item"
            :style="{ background: buttonColor.cancellation, color: buttonColor.color }" @click="cancellation">注销
          </el-button>
          <el-button round size="medium" class="filter-item"
            :style="{ background: buttonColor.reduction, color: buttonColor.color }" @click="reduction">还原</el-button> -->
        </el-form-item>
      </div>

    </el-form>

    <!-- 查询结果 -->
    <el-table :height="buttonColor.clientHeightThree" v-loading="listLoading" :data="list"
      element-loading-text="正在查询中。。。" border fit highlight-current-row :header-cell-style="tableColor"
      @selection-change="handleSelectionChange">
      <el-table-column align="center" :width="buttonColor.operationThree" label="操作" class-name="small-padding">
        <template slot-scope="scope">
          <div class="operationButton">
            <div :style="{ color: buttonColor.edit, cursor: 'pointer' }" @click="handleUpdate(scope.row)">
              <img src="../../../public/images/edit.png" />
              编辑
            </div>
            <div v-if="scope.row.fstate === 0" :style="{ color: buttonColor.delete, cursor: 'pointer' }" @click="handleDelete(scope.row)">
              <img src="../../../public/images/del.png" />
              删除
            </div>
            <div v-else :style="{ color: buttonColor.delete, cursor: 'pointer' }">
              <img src="../../../public/images/del.png" />
              删除
            </div>
            <div v-if="scope.row.fstate === 0" :style="{ color: buttonColor.examine, cursor: 'pointer' }"
              @click="audit(scope.row)">
              <img src="../../../public/images/examine.png" />
              确定
            </div>
            <div v-else :style="{ color: buttonColor.examine, cursor: 'pointer' }">
              <img src="../../../public/images/examine.png" />
              确定
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column :width="buttonColor.line" :index="indexMethod" align="center" label="序" type="index" />
      <el-table-column :width="buttonColor.billno" align="left" label="单号" prop="repoNumber" />
      <el-table-column v-if="this.$store.state.user.fsiteLevel != 3" :formatter="formatfsiteNo" align="left"
        label="客户名称" prop="fsiteNo" />
      <el-table-column width="120" :formatter="formatenterpriseId" align="left" label="处置企业" prop="enterpriseId" />
      <el-table-column :width="buttonColor.user" :formatter="formatforduser" align="left" label="过磅人员"
        prop="weightPerson" />
      <el-table-column :width="buttonColor.date" :formatter="datetimeFormat" align="left" label="过磅日期"
        prop="weightDate" />
      <el-table-column :formatter="formatwasteId" align="left" label="危废名称" prop="wasteId" />
      <el-table-column align="left" label="设备标签" prop="caskName" />
      <el-table-column width="130" align="left" label="固定资产码" prop="fequipmentbarcode" />
      <el-table-column width="130" align="left" label="设备序列号" prop="fmacno" />
      <el-table-column align="left" label="重量" prop="weight" />
      <el-table-column align="left" :width="buttonColor.state" label="状态" prop="fstate">
        <template slot-scope="scope">
          <span :style="{ color: buttonColor.statedisable }" v-if="scope.row.fstate === 0">{{ scope.row.fstate |
              statusFilter
          }}</span>
          <span :style="{ color: buttonColor.statestart }" v-else>{{ scope.row.fstate | statusFilter }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
      @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <el-dialog width="auto" top="0vh" :close-on-click-modal="false" :visible.sync="dialogFormVisible">
      <template slot="title">
        <div :style="titleColor">{{ textMap[dialogStatus] }}</div>
      </template>
      <el-form ref="dataForm" :rules="rules" :model="dataForm" status-icon label-position="left" label-width="auto">
        <el-row>
          <el-col :span="12">
            <el-form-item label="过磅编号" prop="repoNumber">
              <el-input disabled v-model="dataForm.repoNumber" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item class="required" label="过磅日期" prop="weightDate">
              <el-date-picker v-model="dataForm.weightDate" type="date" placeholder="请选择过磅日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item class="required" label="仓库名称" prop="repoId">
              <el-select @change="repoIdChangeAdd" v-model="dataForm.repoId" clearable filterable class="filter-item"
                placeholder="请选择仓库名称">
                <el-option v-for="item in wmslistAdd" :key="item.fstockno" :label="item.fstockname"
                  :value="Number(item.fstockno)" />
              </el-select>
            </el-form-item>

          </el-col>
          <el-col :span="12">
            <el-form-item label="客户名称" prop="fsiteNo">
              <el-select :disabled="fsiteNoFlag" @change="changeFsiteNoAdd" v-model="dataForm.fsiteNo" clearable
                filterable class="filter-item" placeholder="请选择客户名称">
                <el-option v-for="item in getListfsiteNo" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item class="required" label="设备标签" prop="caskId">
              <div style="display:flex;justify-content:flex-end;align-items:center">
                <el-input disabled v-model="dataForm.caskName" placeholder="请输入设备标签"/>
                <i style="display: inline-block;width:20px;color:#409EFF;margin-left:3px" class="el-icon-search"
                  @click="searchDialogcode('add')" />
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="固定资产码" prop="fmacno">
              <el-input disabled v-model="dataForm.fequipmentbarcode" placeholder="请输入固定资产码" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="危废名称" prop="wasteId">
              <el-select disabled v-model="dataForm.wasteId" clearable filterable class="filter-item" placeholder="请选择危废名称">
                <el-option v-for="item in wasteIdList" :key="item.id" :label="item.wasteName" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备序列号" prop="fmacno">
              <el-input disabled v-model="dataForm.fmacno" placeholder="请输入设备序列号" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item class="required" label="过磅重量" prop="weight">
              <el-input v-model="dataForm.weight" placeholder="请输入过磅重量"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item class="searchOne required" label="过磅人员" prop="weightPerson">
              <div style="display:flex;justify-content:flex-start;align-items:center">
                <el-select style="display: inline-block" clearable filterable v-model="dataForm.weightPerson"
                  class="filter-item" placeholder="请选择过磅人员">
                  <el-option v-for="item in getListforduserAdd" :key="item.value" :label="item.label"
                    :value="item.value" />
                </el-select>
                <i class="el-icon-search selectI" @click="searchDialoguse('add')" />
              </div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item class="required" label="处置企业" prop="enterpriseId">
              <el-select v-model="dataForm.enterpriseId" clearable filterable class="filter-item" placeholder="请选择处置企业">
                <el-option v-for="item in enterpriseIdList" :key="item.enterpriseId" :label="item.enterpriseName"
                  :value="item.enterpriseId" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div v-if="viewFlag" slot="footer" class="dialog-footer">
        <el-button round :style="{ background: buttonColor.cancel, color: buttonColor.colorone }"
          @click="dialogFormVisible = false">取消</el-button>
        <el-button round v-if="dialogStatus == 'create'"
          :style="{ background: buttonColor.confirm, color: buttonColor.color }" @click="createData">确定</el-button>
      </div>
      <div v-else slot="footer" class="dialog-footer">
        <el-button round :style="{ background: buttonColor.cancel, color: buttonColor.colorone }"
          @click="dialogFormVisible = false">取消</el-button>
        <el-button round v-if="dialogStatus == 'create'"
          :style="{ background: buttonColor.confirm, color: buttonColor.color }" @click="createData">确定</el-button>
        <el-button round v-else :style="{ background: buttonColor.confirm, color: buttonColor.color }"
          @click="updateData">确定
        </el-button>
      </div>
    </el-dialog>

    <userDialog :searchVisibleuse.sync="searchVisibleuse" :fsiteNo.sync="userDialogFsiteNo" :tableColor="tableColor"
      @userDialog="userDialogValue" />
    <codeDialog v-if="searchVisiblecode" :searchVisiblecode.sync="searchVisiblecode" :fpointNo.sync="codeDialogfpointNo"
      :fsiteNo.sync="codeDialogFsiteNo" :tableColor="tableColor" @codeDialog="codeDialogValue" />

  </div>
</template>

<style>
.button-margin {
  margin: 2px 0px;
}

.el-button+.el-button {
  margin-left: 1px;
}
</style>

<script>
import { listrepositoryManager, createrepositoryManager, updaterepositoryManager, deleterepositoryManager, getInvoicesId, ruaffirm } from '@/api/dangerousWaste/repositoryManager'
import { fsiteOptionsByLevel, getListArea } from '@/api/area'
import { listtsStock } from "@/api/tsStock";
import { listcustomWaste } from '@/api/dangerousWaste/customWaste'
import { listenterpriseBasic } from '@/api/dangerousWaste/enterpriseBasic'

import UserDialog from '@/components/UserDialog'
import CodeDialog from '@/components/CodeDialogChu'

import Pagination from '@/components/Pagination'
import { tableColor, titleColor, buttonColor } from '@/filters/index'
import moment from 'moment'

const statusMap = {
  0: '未确认',
  1: '已确认'
}


export default {
  name: 'WBaseCaskCategoryOut',
  components: { Pagination, UserDialog, CodeDialog },
  filters: {
    statusFilter(type) {
      return statusMap[type]
    }
  },
  data() {
    return {
      statusMap,
      viewFlag: false,
      enterpriseIdList: [], //处置企业
      getListfpointNoAll: [], //地点回显
      getListforduserAll: [], //人员回显
      caskIdDialogFlag: null, // 设备标签添加 搜索标识
      searchVisiblecode: false, // 设备标签--设备台账是否显示
      codeDialogFsiteNo: null, // 设备标签--设备台账客户
      codeDialogfpointNo: null, // 设备标签--设备台账地点
      wasteIdList: [], // 危废名称列表
      wmslistAdd: [], // 仓库列表 添加
      wmslist: [], // 仓库列表 搜索
      searchVisiblelab: false, // 是否显示地点弹窗
      labDialogFsiteNo: null,// 客户编号(地点弹出框)
      labFlag: null, // 判断地点是搜索还是添加
      getListfpointNoAdd: [], // 添加地点列表
      getListfpointNo: [], // 地点列表
      fsiteNoFlag: false, // true非管理员  false管理员
      getListfsiteNo: [], // 客户列表
      userFlag: null, // 判断人员是搜索还是添加
      searchVisibleuse: false, // 是否显示人员弹窗
      userDialogFsiteNo: null, // 客户编号(人员弹出框)
      getListforduserAdd: [], // 添加过磅人员列表
      getListforduser: [], // 过磅人员列表
      alarmdate: null, // 日期查找
      multipleSelection: [], // 多选列表
      tableColor,
      titleColor,
      buttonColor,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        fpointNo: undefined,
        fsiteNo: undefined,
        fstate: undefined,
        repoNumber: undefined,
        weightDate: undefined,
        weightPerson: undefined,
        repoId: undefined,
        wasteId: undefined,
        caskName: undefined,
        caskId: undefined,
        fmacno: undefined,
        fequipmentbarcode: undefined,
        weight: undefined,
        labelNumber: undefined,
        enterpriseId: undefined,
        repoType: 5,
        minTime: undefined,
        maxTime: undefined,
        sort: 'create_time',
        order: 'desc'
      },
      dataForm: {
        fpointNo: undefined,
        fsiteNo: undefined,
        repoNumber: undefined,
        weightDate: undefined,
        weightPerson: undefined,
        repoId: undefined,
        wasteId: undefined,
        caskId: undefined,
        caskName: undefined,
        fmacno: undefined,
        fequipmentbarcode: undefined,
        weight: undefined,
        labelNumber: undefined,
        enterpriseId: undefined,
        fstate: undefined,
        repoType: 5,
        id: ''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      },
      rules: {
        repoNumber: [
          { required: true, message: '过磅编号不能为空' },
        ],
        fsiteNo: [
          { required: true, message: '客户名称不能为空' },
        ],
        fstockno: [
          { required: true, message: '仓库名称不能为空' },
        ],
        caskId: [
          { required: true, message: '设备类型不能为空' },
        ],
        weight: [
          { required: true, message: '重量不能为空' },
        ],
        weightDate: [
          { required: true, message: '过磅时间不能为空' },
        ],
        weightPerson: [
          { required: true, message: '过磅人员不能为空' },
        ],
        repoId: [
          { required: true, message: '仓库编号不能为空' },
        ],
        wasteId: [
          { required: true, message: '危废名称不能为空' },
        ],
        fmacno: [
          { required: true, message: '设备序列号不能为空' },
        ],
        enterpriseId: [
          { required: true, message: '处置企业不能为空' },
        ],
      }
    }
  },
  created() {
    // 查询默认日期
    this.alarmdate = [this.parserDate(moment(new Date().setDate(1)).format('YYYY-MM-DD')), new Date()]
    this.getList()
    // 3学校 客户不能修改
    if (this.$store.state.user.fsiteLevel == 3) {
      this.fsiteNoFlag = true
      this.listQuery.fsiteNo = this.$store.state.user.fsiteNo;
      this.getUserList(this.$store.state.user.fsiteNo)
      this.getPointList(this.$store.state.user.fsiteNo)
      this.listtsStock(this.$store.state.user.fsiteNo)
      this.getUserListAdd(this.$store.state.user.fsiteNo)
      this.getPointListAdd(this.$store.state.user.fsiteNo)
      this.listtsStockAdd(this.$store.state.user.fsiteNo)
    }
    // 人员
    getListArea({ flag: 13 })
      .then(response => {
        this.getListforduserAll = response.data.data.list
      })
      .catch(() => {
        this.getListforduserAll = []
      })

    // 地点
    getListArea({ flag: 10 })
      .then(response => {
        this.getListfpointNoAll = response.data.data.list
      })
      .catch(() => {
        this.getListfpointNoAll = []
      })

    // 客户列表
    fsiteOptionsByLevel(3)
      .then(response => {
        this.getListfsiteNo = response.data.data.list
      })
      .catch(() => {
        this.getListfsiteNo = []
      })

    // 危废名称
    listcustomWaste({ limit: -1, fsiteNo: this.$store.state.user.fsiteNo })
      .then(response => {
        this.wasteIdList = response.data.data.list
      })
      .catch(() => {
        this.wasteIdList = []
      })

    // 处置企业
    listenterpriseBasic({ limit: -1, fsiteNo: this.$store.state.user.fsiteNo})
      .then(response => {
        this.enterpriseIdList = response.data.data.list
      })
      .catch(() => {
        this.enterpriseIdList = []
      })
  },
  methods: {
    // 仓库名称修改
    repoIdChange(data) {
      this.listQuery.caskId = ''
      this.listQuery.caskName = ''
      this.listQuery.fmacno = ''
    },
    // 仓库名称添加修改
    repoIdChangeAdd(data) {
      this.dataForm.caskId = ''
      this.dataForm.caskName = ''
      this.dataForm.fmacno = ''
    },
    // 设备标签--设备台账
    searchDialogcode(value) {
      this.caskIdDialogFlag = value
      if (value == 'add') {
        if (this.dataForm.repoId) {
          this.codeDialogFsiteNo = this.dataForm.fsiteNo
          this.codeDialogfpointNo = this.dataForm.repoId
          this.searchVisiblecode = true
        } else {
          this.$notify.error({
            title: '失败',
            message: '请选择仓库名称'
          })
        }

      } else {
          this.codeDialogFsiteNo = this.listQuery.fsiteNo
          // this.codeDialogfpointNo = this.listQuery.repoId
          this.searchVisiblecode = true
      }
    },

    // 设备标签--固定资产码弹出框返回值
    codeDialogValue(value) {
      console.log('value',value);
      if (this.caskIdDialogFlag == 'add') {
        this.dataForm.caskId = value.info.caskId
        this.dataForm.caskName = value.info.caskName
        this.dataForm.fmacno = value.info.fmacno
        this.dataForm.wasteId = value.info.wasteId
        this.dataForm.weight = value.info.chanshengWeight
        this.dataForm.fequipmentbarcode = value.info.fequipmentbarcode
      } else {
        this.listQuery.caskId = value.info.caskId
        this.listQuery.caskName = value.info.caskName
        this.listQuery.fmacno = value.info.fmacno
        this.listQuery.fequipmentbarcode = value.info.fequipmentbarcode

      }

    },
    // 添加客户修改
    changeFsiteNoAdd(data) {
      this.dataForm.fpointNo = ''
      this.dataForm.weightPerson = ''
      this.getListfpointNoAdd = []
      this.getListforduserAdd = []

      // 仓库名称
      this.wmslistAdd = []
      this.dataForm.repoId = ''
      this.dataForm.caskName = ''
      this.dataForm.caskId = ''
      this.dataForm.fmacno = ''
      this.dataForm.fequipmentbarcode = ''
      if (data) {
        this.getUserListAdd(data)
        this.getPointListAdd(data)
        this.listtsStockAdd(data)
      }
    },
    // 添加仓库
    listtsStockAdd(fsiteNo) {
      listtsStock({ fsiteNo: fsiteNo, ftype: 5, limit: -1 })
        .then((response) => {
          this.wmslistAdd = response.data.data.list;
        })
        .catch(() => {
          this.wmslistAdd = response.data.list;
        });
    },
    // 添加人员列表
    getUserListAdd(fsiteNo) {
      getListArea({ flag: 13, arg1: fsiteNo })
        .then(response => {
          this.getListforduserAdd = response.data.data.list
        })
        .catch(() => {
          this.getListforduserAdd = []
        })
    },
    // 添加存放地点列表
    getPointListAdd(fsiteNo) {
      getListArea({ flag: 10, arg1: fsiteNo })
        .then(response => {
          this.getListfpointNoAdd = response.data.data.list
        })
        .catch(() => {
          this.getListfpointNoAdd = []
        })
    },
      // 设备标签修改
      caskIdNameChange(value){
      if(value){
      }else{
        this.listQuery.caskId = ''
        this.listQuery.fmacno = ''
        this.listQuery.fequipmentbarcode = ''
      }
    },
    // 客户修改
    changeFsiteNo(data) {
      // 地点
      this.listQuery.fpointNo = ''
      this.getListfpointNo = []
      // 人员
      this.listQuery.weightPerson = ''
      this.getListforduser = []
      // 设备标签
      this.listQuery.caskName = ''
      this.listQuery.caskId = ''
      // 序列号
      this.listQuery.fmacno = ''
      this.listQuery.fequipmentbarcode = ''
      // 仓库
      this.listQuery.repoId = ''
      this.wmslist = []

      if (data) {
        this.getUserList(data)
        this.getPointList(data)
        this.listtsStock(data)
      }
    },
    // 人员列表
    getUserList(fsiteNo) {
      getListArea({ flag: 13, arg1: fsiteNo })
        .then(response => {
          this.getListforduser = response.data.data.list
        })
        .catch(() => {
          this.getListforduser = []
        })
    },
    // 存放地点列表
    getPointList(fsiteNo) {
      getListArea({ flag: 10, arg1: fsiteNo })
        .then(response => {
          this.getListfpointNo = response.data.data.list
        })
        .catch(() => {
          this.getListfpointNo = []
        })
    },
    // 仓库
    listtsStock(fsiteNo) {
      listtsStock({ fsiteNo: fsiteNo, ftype: 5, limit: -1 })
        .then((response) => {
          this.wmslist = response.data.data.list;
        })
        .catch(() => {
          this.wmslist = response.data.list;
        });
    },

    // 注销
    cancellation() {
      if (this.multipleSelection.length > 0) {
        let temp = []
        this.multipleSelection.forEach((item) => {
          temp.push(item.caskId)
        })
        setStatus({ ids: temp, status: 0 })
          .then(response => {
            this.$notify.success({
              title: '成功',
              message: '注销成功'
            })
            this.list.forEach(items => {
              if (temp.includes(items.caskId)) {
                items.caskStatus = 0
              }
            })

          })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: response
            })
          })
      } else {
        this.$notify.error({
          title: '失败',
          message: '至少选择一条数据'
        })
      }
    },
    // 还原
    reduction() {
      if (this.multipleSelection.length > 0) {
        let temp = []
        this.multipleSelection.forEach((item) => {
          temp.push(item.caskId)
        })
        setStatus({ ids: temp, status: 1 })
          .then(response => {
            this.$notify.success({
              title: '成功',
              message: '还原成功'
            })
            this.list.forEach(items => {
              if (temp.includes(items.caskId)) {
                items.caskStatus = 1
              }
            })
          })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: response
            })
          })
      } else {
        this.$notify.error({
          title: '失败',
          message: '至少选择一条数据'
        })
      }
    },
    // 多选事件
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 处置企业
    formatenterpriseId: function (row, column) {
      if (this.enterpriseIdList) {
        for (let i = 0; i < this.enterpriseIdList.length; i++) {
          if (row.enterpriseId == this.enterpriseIdList[i].enterpriseId) {
            return this.enterpriseIdList[i].enterpriseName
          }
        }
        return ''
      } else {
        return ''
      }
    },

    // 危废名称
    formatwasteId: function (row, column) {
      if (this.wasteIdList) {
        for (let i = 0; i < this.wasteIdList.length; i++) {
          if (row.wasteId == this.wasteIdList[i].id) {
            return this.wasteIdList[i].wasteName
          }
        }
        return ''
      } else {
        return ''
      }
    },
    // 搜索人员弹出框
    searchDialoguse(value) {
      this.userFlag = value
      if (value === 'add') {
        this.userDialogFsiteNo = this.dataForm.fsiteNo
      } else {
        this.userDialogFsiteNo = this.listQuery.fsiteNo
      }
      this.searchVisibleuse = true
    },

    // 人员弹出框返回值
    userDialogValue(value) {
      if (this.userFlag === 'add') {
        this.dataForm.weightPerson = value.user.id.toString()
      } else {
        this.listQuery.weightPerson = value.user.id.toString()
      }
    },
    // 人员
    formatforduser: function (row, column) {
      if (this.getListforduserAll) {
        for (let i = 0; i < this.getListforduserAll.length; i++) {
          if (row.weightPerson == this.getListforduserAll[i].value) {
            return this.getListforduserAll[i].label
          }
        }
        return ''
      } else {
        return ''
      }
    },
    // 搜索地点弹出框
    searchDialoglab(value) {
      this.labFlag = value
      if (value === 'add') {
        this.labDialogFsiteNo = this.dataForm.fsiteNo
      } else {
        this.labDialogFsiteNo = this.listQuery.fsiteNo
      }
      this.searchVisiblelab = true
    },
    // 客户回显
    formatfsiteNo: function (row, column) {
      if (this.getListfsiteNo) {
        for (let i = 0; i < this.getListfsiteNo.length; i++) {
          if (row.fsiteNo === this.getListfsiteNo[i].value) {
            return this.getListfsiteNo[i].label
          }
        }
        return ''
      } else {
        return ''
      }
    },
    datetimeFormat(row, column) {
      const date = row[column.property]
      if (date === undefined || date === null) {
        return ''
      }
      return moment(date).format('YYYY-MM-DD')
    },
    getList() {
      this.listQuery.minTime = this.alarmdate == null ? null : this.formatDate(this.alarmdate[0]) + '00:00:00'
      this.listQuery.maxTime = this.alarmdate == null ? null : this.formatDate(this.alarmdate[1]) + '23:59:59'
      this.listLoading = true
      listrepositoryManager(this.listQuery)
        .then(response => {
          this.list = response.data.data.list
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resetForm() {
      this.dataForm = {
        repoNumber: undefined,
        weightDate: undefined,
        weightPerson: undefined,
        repoId: undefined,
        wasteId: undefined,
        caskId: undefined,
        caskName: undefined,
        fmacno: undefined,
        fequipmentbarcode: undefined,
        weight: undefined,
        labelNumber: undefined,
        enterpriseId: undefined,
        fstate: undefined,
        repoType: 5,
        id: ''
      }
    },
    handleCreate() {
      this.resetForm()

      // 单据编号
      getInvoicesId()
        .then((response) => {
          if (response) {
            this.dataForm.repoNumber = response.data.data.repoNumber
            this.dialogFormVisible = true
            this.$nextTick(() => {
              this.$refs['dataForm'].clearValidate()
            })
          }
        })
        .catch(() => {
          this.dataForm.repoNumber = ''
        })
      // 默认日期
      this.dataForm.weightDate = new Date()
      this.dialogStatus = 'create'
      if (this.$store.state.user.fsiteLevel == 3) {
        this.dataForm.weightPerson = this.$store.state.user.id.toString()
        this.dataForm.fsiteNo = this.$store.state.user.fsiteNo;
        this.dataForm.repoId = Number(this.wmslistAdd[0].fstockno)
        this.listtsStockAdd(this.$store.state.user.fsiteNo)
      }
    },
    createData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.dataForm.fstate = 0
          createrepositoryManager(this.dataForm)
            .then(response => {
              this.list.unshift(response.data.data)
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '添加出库过磅成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response
              })
            })
        }
      })
    },
    handleUpdate(row) {
      if (row.fstate == '0') {
        this.viewFlag = false
      } else {
        this.viewFlag = true
      }
      this.dataForm = Object.assign({}, row)
      this.dataForm.caskName = row.caskName
      this.dataForm.repoId = Number(this.dataForm.repoId)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      if (this.$store.state.user.fsiteLevel != 3) {
        this.getUserListAdd(row.fsiteNo)
        this.getPointListAdd(row.fsiteNo)
      }
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          updaterepositoryManager(this.dataForm, this.$route.query.id)
            .then(() => {
              for (const v of this.list) {
                if (v.id === this.dataForm.id) {
                  const index = this.list.indexOf(v)
                  this.list.splice(index, 1, this.dataForm)
                  break
                }
              }
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '更新出库过磅成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response
              })
            })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('是否要删除出库过磅', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleterepositoryManager(row)
          .then(response => {
            this.$notify.success({
              title: '成功',
              message: '删除出库过磅成功'
            })
            const index = this.list.indexOf(row)
            this.list.splice(index, 1)
          })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: response
            })
          })
      })
    },
    audit(row) {
      this.$confirm("是否确定", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.dataForm = Object.assign({}, row);
        ruaffirm({
          repoNumber: this.dataForm.repoNumber,
        })
          .then(() => {
            for (const v of this.list) {
              if (v.repoNumber === this.dataForm.repoNumber) {
                this.dataForm.fstate = 1;
                const index = this.list.indexOf(v);
                this.list.splice(index, 1, this.dataForm);
                break;
              }
            }
            this.$notify.success({
              title: "成功",
              message: "确认成功",
            });
          })
          .catch((response) => {
            this.$notify.error({
              title: "失败",
              message: response.data.data,
            });
          });
      }).catch(() => { });
    },
    indexMethod(index) {
      index = index + 1
      return index
    },
    parserDate(date) {
      var t = Date.parse(date)
      if (!isNaN(t)) {
        return new Date(Date.parse(date.replace(/-/g, '/')))
      }
    },
    formatDate: function (date) {
      if (typeof (date) === 'object') {
        var ym
        var y = date.getFullYear()
        var m = date.getMonth() + 1
        var d = date.getDate()
        if (m < 10) {
          m = '0' + m
        }
        if (d < 10) {
          d = '0' + d
        }
        ym =y + '-' + m + '-' + d
        return ym
      } else {
        return date
      }
    },
  }
}
</script>
